package com.mjt.admin.controller;


import com.mjt.admin.model.RespBean;
import com.mjt.admin.model.RespDataBean;
import com.mjt.admin.pojo.Goods;
import com.mjt.admin.pojo.GoodsType;
import com.mjt.admin.query.GoodsQuery;
import com.mjt.admin.service.IGoodsService;
import com.mjt.admin.service.IGoodsTypeService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 莫某人
 * @since 2021-11-01
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {


    @Resource
    private IGoodsService goodsService;

    @Resource
    private IGoodsTypeService iGoodsTypeService;

    /**
     * @description 跳转到商品管理主页
     * @author 莫某人
     * @date 9:23 2021/11/2
     * @return {@link String}
     **/
    @RequestMapping("index")
    public String index(){
        return "/goods/goods";
    }



    /**
     * @description 获取商品信息列表
     * @author 莫某人
     * @date 9:56 2021/11/2
 * @param goodsQuery
     * @return {@link RespDataBean< Goods>}
     **/
    @RequestMapping("list")
    @ResponseBody
    public RespDataBean<Goods>  goodsList(GoodsQuery goodsQuery){

        return goodsService.goodsList(goodsQuery);

    }

    /**
     * @description 打开商品添加|修改页面
     * @author 莫某人
     * @date 11:44 2021/11/2
 * @param id
 * @param model
     * @return {@link String}
     **/
    @RequestMapping("addOrUpdateGoodsPage")
    public String addOrUpdateGoodsPage(Integer id, Model model){

        if (id==null){
            //添加页面
            return "goods/add_update";
        }else {
            //修改页面
            Goods goods = goodsService.getById(id);
            GoodsType goodsType = iGoodsTypeService.getById(goods.getTypeId());
            model.addAttribute("goods",goods);
            model.addAttribute("goodsType",goodsType);
            return "goods/add_update";
        }

    }

    /**
     * @description 打开商品管理页面
     * @author 莫某人
     * @date 17:50 2021/11/2
     * @return {@link String}
     **/
    @RequestMapping("toGoodsTypePage")
    public String toGoodsTypePage(){
        return "goods/goods_type";
    }

    /**
     * @description 添加商品信息
     * @author 莫某人
     * @date 8:52 2021/11/3
 * @param goods
     * @return {@link RespBean}
     **/
    @RequestMapping("save")
    @ResponseBody
    public RespBean saveGoods(Goods goods){
         goodsService.saveGoods(goods);

         return RespBean.success("添加成功");
    }

    /**
     * @description 修改商品信息
     * @author 莫某人
     * @date 8:54 2021/11/3
 * @param goods
     * @return {@link RespBean}
     **/
    @RequestMapping("update")
    @ResponseBody
    public RespBean updateGoods(Goods goods){
        goodsService.updateGoods(goods);

        return RespBean.success("修改成功");

    }


    /**
     * @description 删除商品信息
     * @author 莫某人
     * @date 9:09 2021/11/3
 * @param id
     * @return {@link RespBean}
     **/
    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleteGoods(Integer id){
        goodsService.deleteGoods(id);
        return RespBean.success("删除成功");
    }






}
